//
//  UpdatePwdController.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/4/27.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import UIKit
import Alamofire
import SwiftyJSON
//修改密码
class UpdatePwdController: BaseController {
    var personal:PersonalModel!//个人信息
    var oldPwd,userPwd:UITextField!//用户名密码
    var codebutton : UIButton!
    let textX:CGFloat = 20
    let textWidth:CGFloat = kScreenW-40
    override func viewDidLoad(){
        super.viewDidLoad()
        setupNavBar(title: "修改密码", isback: true, appearWay: 3, isright: false)//自定义导航
        //原始密码
        oldPwd = UITextField(frame: CGRect(x:textX, y:kNavigationBarH + kStatusBarH + 30, width:textWidth, height:cellH))
        oldPwd.placeholder = "请输入旧密码"
        oldPwd.autocapitalizationType = .none
        //设置左边小图片
        let phoneLeftView:UIView=UIView(frame: CGRect(x:0, y:0, width:30, height:40));
        let leftImage1:UIImageView=UIImageView(frame: CGRect(x:5,y:10, width:18, height:20));
        leftImage1.image=UIImage(named: "password");
        phoneLeftView.addSubview(leftImage1)
        oldPwd.leftView = phoneLeftView//  设置左边小图片
        oldPwd.leftViewMode = UITextFieldViewMode.always
        oldPwd.isSecureTextEntry = true
        self.view.addSubview(oldPwd)
        //分割线
        let bseparator2 = UILabel.init(frame: CGRect(x: textX, y: kNavigationBarH + kStatusBarH + 70, width: textWidth + 20, height: 1))
        bseparator2.backgroundColor = separatorColor
        self.view.addSubview(bseparator2)
        //密码
        userPwd = UITextField(frame: CGRect(x:textX, y:kNavigationBarH + kStatusBarH + 80, width:textWidth, height:cellH))
        userPwd.placeholder = "请输入新密码"
        userPwd.autocapitalizationType = .none
        let pwdLeftView:UIView=UIView(frame: CGRect(x:0, y:0, width:30, height:40));
        let leftImage2:UIImageView=UIImageView(frame: CGRect(x:5,y:10, width:18, height:20));
        leftImage2.image=UIImage(named: "password");
        pwdLeftView.addSubview(leftImage2)
        userPwd.leftView = pwdLeftView//  设置左边小图片
        userPwd.leftViewMode = UITextFieldViewMode.always
        userPwd.isSecureTextEntry = true
        self.view.addSubview(userPwd)
        //分割线
        let bseparator3 = UILabel.init(frame: CGRect(x: textX, y: kNavigationBarH + kStatusBarH + 120, width: textWidth + 20, height: 1))
        bseparator3.backgroundColor = separatorColor
        self.view.addSubview(bseparator3)
        //确定按钮
        let button:UIButton = UIButton(type:.custom)
        button.frame = CGRect(x:textX, y:kNavigationBarH + kStatusBarH + 170, width:textWidth, height:40)//设置登陆按钮位置和大小
        button.layer.cornerRadius = 10;
        button.backgroundColor = appMainColor
        button.showsTouchWhenHighlighted = true //
        button.adjustsImageWhenHighlighted=false ////使触摸模式下按钮也不会变暗（半透明
        button.setTitle("确定", for:.normal)
        button.addTarget(self, action: #selector(updatePwdBtnClick), for: UIControlEvents.touchUpInside)
        self.view.addSubview(button)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
}
extension UpdatePwdController{
    //修改密码
    func updatePwdBtnClick(){
        if "" == userPwd.text! || "" ==  oldPwd.text! {
            checkAlertFun(message: "新密码、旧密码不能为空！")
        }else{
            confirmFun()
        }
    }
    //提交修改
    func confirmFun(){
        if reachability.isReachable {
            let parameter:[String:Any] = [
                "userFiled":"upasswd",
                "updateValue":userPwd.text!,
                "oldPasswd":oldPwd.text!
            ]
            Alamofire.request("\(projectUrl)/user/updateUserCoreInfo/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)",method:.post, parameters:parameter,headers:headers).responseJSON { (response) in
                let jsonString = response.result.value!
                let jsonDate = JSON(jsonString)
                if jsonDate["success"] == true {
                    let alertController = UIAlertController(title: alertTitle, message: "密码修改成功！", preferredStyle: .alert)
                    let okAction = UIAlertAction(title: "重新登录", style: .default, handler:{
                        (UIAlertAction) -> Void in
                        let mainVC = UINavigationController(rootViewController: LoginController())
                        app.window?.rootViewController = mainVC
                    })
                    alertController.addAction(okAction)
                    self.present(alertController, animated: true, completion: nil)
                }else{
                    self.failOperNoticeFun(code: jsonDate["rcode"].rawString()!, message: jsonDate["rdata"].rawString()!)
                }
            }
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}
